var app=angular.module("manage",["ngAnimate","ui.router","oc.lazyLoad","ui.bootstrap","ng-echarts","ui.tinymce","ngTreetable"
,"Encrypt","ui.select","treeControl","pasvaz.bindonce","ngFileUpload","ngCookies"]);

var Authorization= localStorage.getItem("Authorization");
var url= localStorage.getItem("page");
if(Authorization==null||url==null){
	window.location.href="index.html#!/login"
}
app.config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
    function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
        app.controller = $controllerProvider.register;
        app.directive = $compileProvider.directive;
        app.filter = $filterProvider.register;
        app.factory = $provide.factory;
        app.service = $provide.service;
        app.constant = $provide.constant;
    }]);
app.config(function ($httpProvider) {
    $httpProvider.defaults.transformRequest = function (obj) {
        var str = [];
        for (var p in obj) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
        return str.join("&");
    };
 	if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};
    }
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.post = {
        'Content-Type': 'application/x-www-form-urlencoded; charser=UTF-8'
    }
    
    
 	 $httpProvider.defaults.headers.common['Authorization'] = Authorization;
});

app.constant('Modules_Config', [
    {
        name: 'treeControl',
        serie: true,
        files: []
    }
]);
// 禁止模板缓存
app.run(function($rootScope, $templateCache) {
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if (typeof(current) !== 'undefined'){
            $templateCache.remove(current.templateUrl);
        }
    });
});

app.filter('fil', function() {
	return function(vl) {
		if(vl != null) {
			if(vl.length > 10) {
				return vl.substr(0, 10) + "......";
			} else {
				return vl;
			}
		}
	}
});
app.filter('imgs', function() {
	return function(vl) {
		console.log(vl)
		if(vl != null) {
			
				return localStorage.getItem("Url") + vl;
		}
	}
});
app.config(["$ocLazyLoadProvider","Modules_Config",routeFn]);
function routeFn($ocLazyLoadProvider,Modules_Config){
    $ocLazyLoadProvider.config({
        debug:false,
        events:false,
        modules:Modules_Config
    });
};
app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);
 
 app.directive("datetimepicker",function(){
    return {
    	 require : "ngModel",
        restrict: "EA",
        template:'<input id="datetimepicker"  ng-value="pickertime"  autocomplete="off">',
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					
					setTimeout(function(){
					},200)
                    $("#datetimepicker").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  
					        scope.pickertime = result;  
					        scope.add.entryTime=result
					        scope.$apply();  
					    });  
        }
    }
});
 app.directive("modelPicker",function(){
    return {
    	 require : "ngModel",
        restrict: "EA",
        template:'<input id="datetimepicker"  class="table_input" ng-value="pickertime"  autocomplete="off">',
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					
					setTimeout(function(){
					},200)
                    $("#datetimepicker").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  
					        scope.pickertime = result;  
					        scope.$apply();  
					    });  
        }
    }
});
 app.directive("modelPickers",function(){
    return {
    	 require : "ngModel",
        restrict: "EA",
        template:'<input id="modelPickers"  class="table_input" ng-value="pickertimes"  autocomplete="off">',
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					
					setTimeout(function(){
					},200)
                    $("#modelPickers").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  
					        scope.pickertimes = result;  
					        scope.$apply();  
					    });  
        }
    }
});
 app.directive("timePice",function(){
    return {
    	require : "ngModel",
        restrict: "EA",
        template:'<input id="datetims" ng-value="pickertimes" autocomplete="off" >',
       
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					setTimeout(function(){
					},200)
                    $("#datetims").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  
					        scope.add.working = result;  
					        scope.$apply();  
					    });  
        }
    }
});

 app.directive("startTime",function(){
    return {
    	 require : "ngModel",
        restrict: "EA",
        template:'<input id="startTime" class="form-control inputweight"   ng-value="pickertime"  autocomplete="off">',
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					
					setTimeout(function(){
					},200)
                    $("#startTime").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  

					        if(result=='Invalid date'){
								 scope.startTime = '';  
							}else{
								 scope.startTime = result;  
							}
					        scope.add.entryTime=result
					        scope.$apply();  
					    });  
        }
    }
});
 app.directive("endTime",function(){
    return {
    	 require : "ngModel",
        restrict: "EA",
        template:'<input id="endTime"  class="form-control inputweight"  ng-value="pickertime"  autocomplete="off">',
        link: function (scope, element, attrs, ctrl) {
					//获取当前时间\
					
						
					setTimeout(function(){
					},200)
                    $("#endTime").datetimepicker({
							format: 'YYYY-MM-DD',
						}).on('dp.change', function (e) {  
					        var result = new moment(e.date).format('YYYY-MM-DD');  
							console.log(result)
							if(result=='Invalid date'){
								 scope.endTime = '';  
							}else{
								 scope.endTime = result;  
							}
					       
					        scope.add.entryTime=result
					        scope.$apply();  
					    });  
        }
    }
});
//过滤长度		
app.filter('fil', function() {
	return function(vl) {
		if(vl != null) {
			if(vl.length > 15) {
				return vl.substr(0, 15) + "...";
			} else {
				return vl;
			}
		}
	}
});

//付款类型	
app.filter('fee', function() {
	return function(vl) {
		if(vl != null) {
			if(vl=="0"||vl==0) {
				return "现金";
			} else {
				return "银行存款";
			}
		}
	}
});
//收入类型	
app.filter('sr', function() {
	return function(vl) {
		if(vl != null) {
			if(vl=="0"||vl==0) {
				return "政府补贴";
			} else {
				return "无合同项目";
			}
		}
	}
});
app.filter('age', function() {
	return function(vl) {
		if(vl != null) {
			vl+="岁"
		}
		return vl;
	}
});
app.filter('year', function() {
	return function(vl) {
		if(vl != null) {
			vl+="年"
		}
		return vl;
	}
});

//过滤长度		
app.filter('value', function() {
	return function(collection, keyname,value) {
	
		if(keyname!=null){
			angular.forEach(value,function(o){
				if(o.diId==keyname){
					console.log(o.diName)
					return o.diName
				}
			})
			
		}
		
	}
});
//过滤长度		
app.filter('dateValue', function() {
	return function(value) {
		if(value!=null){
					return value.substring(0,10)
		}
		
	}
});


Date.prototype.format = function(format)
{
 var o = {
 "M+" : this.getMonth()+1, //month
 "d+" : this.getDate(),    //day
 "h+" : this.getHours(),   //hour
 "m+" : this.getMinutes(), //minute
 "s+" : this.getSeconds(), //second
 "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
 "S" : this.getMilliseconds() //millisecond
 }
 if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
 (this.getFullYear()+"").substr(4 - RegExp.$1.length));
 for(var k in o)if(new RegExp("("+ k +")").test(format))
 format = format.replace(RegExp.$1,
 RegExp.$1.length==1 ? o[k] :
 ("00"+ o[k]).substr((""+ o[k]).length));
 return format;
}